package org.jeecg.modules.filebank.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xkcoding.http.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.filebank.entity.CrdFileBli;
import org.jeecg.modules.filebank.entity.CrdLibShare;
import org.jeecg.modules.filebank.service.ICrdFileBliService;
import org.jeecg.modules.filebank.service.ICrdLibShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
* @Description: 文件库分享
* @Author: jeecg-boot
* @Date:   2020-06-18
* @Version: V1.0
*/
@Slf4j
@Api(tags="文件库分享")
@RestController
@RequestMapping("/fileBank/share")
public class CrdLibShareController extends JeecgController<CrdLibShare, ICrdLibShareService> {
   @Autowired
   private ICrdLibShareService CrdLibShareService;
   @Autowired
   ICrdFileBliService crdFileBliService;


   /**
    * 添加
    *
    * @param
    * @return
    */
   @AutoLog(value = "文件库分享-添加")
   @ApiOperation(value="文件库分享-添加", notes="文件库分享-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestParam(name="libId", required = true) String libId, HttpServletRequest req) {
       LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
       CrdLibShare crdLibShare=new CrdLibShare();
       if (!StringUtil.isEmpty(libId)){
           crdLibShare.setLibId(libId);
       }
       crdLibShare.setUserId(sysUser.getId());
       crdLibShare.setAvatar(sysUser.getAvatar());
       crdLibShare.setUsername(sysUser.getUsername());
       crdLibShare.setUrlCode(UUIDGenerator.generate());
       crdLibShare.setCode(RandomStringUtils.randomAlphanumeric(4));
       CrdLibShareService.save(crdLibShare);
       crdLibShare.setUrlCode(crdLibShare.getUrlCode());
       return Result.ok(crdLibShare);
   }

    /**
     * 编辑
     *
     * @param
     * @return
     */
    @AutoLog(value = "文件库分享-获取分享人")
    @ApiOperation(value="文件库分享-获取分享人", notes="文件库分享-获取分享人")
    @PostMapping(value = "/queryShareUser")
    public Result<?> queryShareUser(@RequestParam(name="urlCode", required = true) String urlCode,
                                   HttpServletRequest req) {
        QueryWrapper<CrdLibShare> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url_code",urlCode);
        CrdLibShare crdLibShareRow=CrdLibShareService.getOne(queryWrapper);
        if (crdLibShareRow==null){
            return  Result.error("资源不存在!");
        }

        Result<JSONObject> result=new Result<>();
        // 获取用户部门信息
        JSONObject obj = new JSONObject();
        obj.put("username",crdLibShareRow.getUsername());
        obj.put("avatar",crdLibShareRow.getAvatar());
        result.setResult(obj);
        result.setCode(200);
        result.success("获取分享人信息成功！");

        return result;
    }

    /**
     * 编辑
     *
     * @param
     * @return
     */
    @AutoLog(value = "文件库分享-查询文件分享资源")
    @ApiOperation(value="文件库分享-查询文件分享资源", notes="文件库分享-查询文件分享资源")
    @PostMapping(value = "/queryShareLib")
    public Result<?> queryShareLib(@RequestParam(name="urlCode", required = true) String urlCode,
                                   @RequestParam(name="code", required = true) String code,
                                   HttpServletRequest req) {
        QueryWrapper<CrdLibShare> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("url_code",urlCode);


        CrdLibShare crdLibShareRow=CrdLibShareService.getOne(queryWrapper);
        if (crdLibShareRow==null){
            return  Result.error("资源不存在!");
        }
        if (!code.equals(crdLibShareRow.getCode())){
            return Result.error("提取码错误!");
        }

        CrdFileBli crdFileBli=crdFileBliService.getById(crdLibShareRow.getLibId());

        Result<JSONObject> result=new Result<>();
        // 获取用户部门信息
        JSONObject obj = new JSONObject();
        obj.put("crdLibShare",crdLibShareRow);
        obj.put("crdFileLib",crdFileBli);
        result.setResult(obj);
        result.setCode(200);
        result.success("获取分享成功！");
        return result;
    }


}
